package io.renren.modules.sys.customer.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.customer.entity.BtCustomerBaseEntity;
import io.renren.modules.sys.customer.service.BtCustomerBaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.customer.entity.BtCustomerMessageEntity;
import io.renren.modules.sys.customer.service.BtCustomerMessageService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author goudandan
 * @email resjz1@163.com
 * @date 2018-11-09 09:47:34
 */
@RestController
@RequestMapping("sys/btcustomermessage")
public class BtCustomerMessageController extends AbstractController {
    @Autowired
    private BtCustomerMessageService btCustomerMessageService;
    @Autowired
    private BtCustomerBaseService btCustomerBaseService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:btcustomermessage:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = btCustomerMessageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:btcustomermessage:info")
    public R info(@PathVariable("id") Long id){
        BtCustomerMessageEntity btCustomerMessage = btCustomerMessageService.selectById(id);

        return R.ok().put("btCustomerMessage", btCustomerMessage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:btcustomermessage:save")
    public R save(@RequestBody BtCustomerMessageEntity btCustomerMessage){
        Long uid = getUserId();
        if(uid.equals(0)){
            return R.error("非法用户！！");
        }
        BtCustomerBaseEntity btCustomerBase = btCustomerBaseService.SelectOneByUid(uid);
        if(btCustomerBase==null){
            return R.error("没有该店铺！");
        }
        String ec="";
        try {
            ec = URLDecoder.decode(btCustomerMessage.getDetail(),"utf-8") ;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        btCustomerMessage.setDetail(ec);
        btCustomerMessage.setCustId(btCustomerBase.getId());
        btCustomerMessageService.insert(btCustomerMessage);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:btcustomermessage:update")
    public R update(@RequestBody BtCustomerMessageEntity btCustomerMessage){
        ValidatorUtils.validateEntity(btCustomerMessage);
        Long uid = getUserId();
        if(uid.equals(0)){
            return R.error("非法用户！！");
        }
        BtCustomerBaseEntity btCustomerBase = btCustomerBaseService.SelectOneByUid(uid);
        if(btCustomerBase==null){
            return R.error("没有该店铺！");
        }
        String ec="";
        try {
            ec = URLDecoder.decode(btCustomerMessage.getDetail(),"utf-8") ;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        btCustomerMessage.setDetail(ec);
        btCustomerMessage.setCustId(btCustomerBase.getId());
        btCustomerMessageService.updateAllColumnById(btCustomerMessage);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:btcustomermessage:delete")
    public R delete(@RequestBody Long[] ids){
        btCustomerMessageService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
